package com.ywq.wechatordersys.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ywq.wechatordersys.data.OrderDetail;
import com.ywq.wechatordersys.dto.OrderDTO;
import com.ywq.wechatordersys.form.OrderForm;
import com.ywq.wechatordersys.utils.JsonUtil;

import java.lang.reflect.Type;
import java.util.List;

public class OrderForm2OrderDTOConverter {


    public static OrderDTO converter(OrderForm orderForm) {
        OrderDTO orderDTO = new OrderDTO();

        String name = orderForm.getName();
        String address = orderForm.getAddress();
        String phone = orderForm.getPhone();
        String openid = orderForm.getOpenid();
        String items = orderForm.getItems();

        orderDTO.setBuyerName(name);
        orderDTO.setBuyerPhone(phone);
        orderDTO.setBuyerOpenid(openid);
        orderDTO.setBuyerAddress(address);
        //涉及到json转换
        Type type = new TypeToken<List<OrderDetail>>() {
        }.getType();
        List<OrderDetail> orderDetailList = JsonUtil.json2Bean(items, type);
        orderDTO.setOrderDetailList(orderDetailList);
        return orderDTO;
    }


}
